import worker, { ThreadWorkerGlobalScope, MessageEvents, ErrorEvent } from '@ohos.worker';
import Handle from './handle'  // 返回句柄
const workerPort: ThreadWorkerGlobalScope = worker.workerPort;
// 无法传输的句柄，所有操作依赖此句柄
let handler: Handle = new Handle()
/**
 * Defines the event handler to be called when the worker thread receives a message sent by the host thread.
 * The event handler is executed in the worker thread.
 *
 * @param e message data
 */
workerPort.onmessage = function (e: MessageEvents) {
  switch (e.data.type as number) {
    case 0:
      handler.syncSet(e.data.data);
      workerPort.postMessage('success set');
      console.info("worker type0")
    case 1:
      handler.syncGet();
      workerPort.postMessage('success get');
      console.info("worker type1")
  }
}

/**
 * Defines the event handler to be called when the worker receives a message that cannot be deserialized.
 * The event handler is executed in the worker thread.
 *
 * @param e message data
 */
workerPort.onmessageerror = function (e: MessageEvents) {
}

/**
 * Defines the event handler to be called when an exception occurs during worker execution.
 * The event handler is executed in the worker thread.
 *
 * @param e error message
 */
workerPort.onerror = function (e: ErrorEvent) {
}